<?php

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class TravelCouponUser extends Model
{
    use SoftDelete;
    // Define the table name
    protected $name = 'travel_coupon_users';
    public $timestamps = false;
    protected $deleteTime = 'deleted_at';

    public function coupon()
    {
        return $this->hasOne(TravelCoupon::class, 'id', 'coupon_id');
    }

    // 获取待使用优惠卷的总金额
    public function getCouponAmount($uid){
        $amount = $this->alias('a1')
        ->leftJoin('travel_coupons a2','a1.coupon_id = a2.id')
        ->where('a1.user_id',$uid)
        ->where('a1.status',0)
        ->where('a2.status',2)
        ->sum('discount_amount');
        return $amount;
    }
}
